<template>
  <el-dialog title="社群详情" v-model="visible" width="800px" @close="onClose">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进群方式" prop="joinScene">
        <el-select v-model="queryParams.joinScene" fit-input-width clearable placeholder="请选择">
          <el-option
            width="300px"
            v-for="item in joinSceneOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select
          v-model="queryParams.gender"
          style="width: 100px"
          fit-input-width
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in genderOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进群时间" prop="joinTime">
        <el-date-picker
          style="width: 240px"
          v-model="queryParams.joinTime"
          type="daterange"
          value-format="x"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" size="default" index :data="dataList">
      <el-table-column type="index" :index="indexAdd" label="编号" width="50" align="center" />
      <el-table-column label="用户名称" align="center" prop="name" />
      <el-table-column label="性别" min-width="150px" align="center" prop="gender">
        <template #default="{ row }">
          <span>{{ getGender(row.gender) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进群方式" min-width="150px" align="center" prop="joinScene">
        <template #default="{ row }">
          <span>{{ getJoninType(row.joinScene) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进群时间" min-width="150px" align="center">
        <template #default="{ row }">
          <span>{{ getJoinTime(row.joinTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-right: 10px">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </el-dialog>
</template>

<script setup>
import { getGroupMemberList } from '@/api/association'
import { dayjs } from 'element-plus'
const queryRef = ref(null)

const total = ref(0)
const dataList = ref([])
const visible = ref(false)

const queryParams = reactive({
  name: '',
  joinScene: '',
  gender: '',
  joinTime: [],
  groupChatId: '',
  pageNum: 1,
  pageSize: 10
})

const joinSceneOptions = [
  {
    label: '直接邀请入群',
    value: 1
  },
  {
    label: '邀请链接入群',
    value: 2
  },
  {
    label: '群二维码入群',
    value: 3
  }
]
const genderOptions = [
  {
    label: '未知',
    value: '0'
  },
  {
    label: '男性',
    value: '1'
  },
  {
    label: '女性',
    value: '2'
  }
]
const loading = ref(true)

function getGender(gender) {
  const obj = genderOptions.find((i) => {
    return i.value === gender
  })
  return obj ? obj.label : '未知'
}
function getJoninType(joinType) {
  const obj = joinSceneOptions.find((i) => {
    return i.value === joinType
  })
  return obj ? obj.label : ''
}
function getJoinTime(joinTime) {
  return joinTime ? dayjs(joinTime * 1000).format('YYYY-MM-DD HH:mm:ss') : ''
}

// 表格编号递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams
  return index + 1 + (pageNum - 1) * pageSize
}

/** 查询用户列表 */
function getList() {
  loading.value = true
  const params = {
    ...queryParams,
    startTime: queryParams.joinTime?.length ? queryParams.joinTime[0] / 1000 : '',
    endTime: queryParams.joinTime?.length ? queryParams.joinTime[1] / 1000 : ''
  }
  delete params.joinTime
  getGroupMemberList(params)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

function init(id) {
  visible.value = true
  queryParams.groupChatId = id
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
</style>
